package com.example.vue.controller;

import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/api")
public class ProductController {

    @PostMapping("/search/getSearchInfoList")
    public Result<PageData> getProducts(@RequestBody ProductSearchParam param) {

        // 1. 模拟品牌数据
        List<Trademark> trademarkList = Arrays.asList(
                new Trademark(1, "苹果"),
                new Trademark(2, "华为"),
                new Trademark(3, "小米"),
                new Trademark(4, "OPPO"),
                new Trademark(5, "vivo"),
                new Trademark(6, "三星"),
                new Trademark(7, "荣耀")
        );

        // 2. 模拟属性数据
        List<Attr> attrsList = Arrays.asList(
                new Attr(1, "价格", Arrays.asList("4500-11999", "2800-4499", "1000-2799")),
                new Attr(2, "屏幕尺寸", Arrays.asList("5.5英寸", "6.1英寸", "6.7英寸", "7.0英寸")),
                new Attr(3, "内存容量", Arrays.asList("64GB", "128GB", "256GB", "512GB")),
                new Attr(4, "摄像头", Arrays.asList("单摄", "双摄", "三摄", "四摄")),
                new Attr(5, "电池容量", Arrays.asList("3000mAh", "4000mAh", "5000mAh"))
        );

        // 3. 模拟商品数据 (共10条)
        List<Goods> goodsList = new ArrayList<>();
        for (int i = 1; i <= 10; i++) {
            goodsList.add(new Goods(
                    (long) i,
                    "https://img13.360buyimg.com/n7/jfs/t1/319946/33/6083/39060/683ede35F7de500fa/c690fc1da56954f4.png",
                    "智能手机型号" + (char)(64 + i),
                    new BigDecimal(2000 + (i * 500)),
                    new Date(),
                    i % 7 + 1, // tmId 在1-7之间循环
                    getBrandName(i % 7 + 1), // 根据tmId获取品牌名
                    null, null, null, null, null, null, // 分类信息设为null
                    80 + i * 5, // 热度分数
                    null // 属性列表设为null
            ));
        }

        // 4. 创建分页数据
        long total = 26; // 总记录数
        PageData pageData = new PageData(
                trademarkList,
                attrsList,
                goodsList,
                total,
                param.getPageSize(),
                param.getPageNo()
        );

        // 5. 返回成功响应
        return Result.success(pageData);
    }

    // 根据品牌ID获取品牌名称
    private String getBrandName(int tmId) {
        switch (tmId) {
            case 1: return "苹果";
            case 2: return "华为";
            case 3: return "小米";
            case 4: return "OPPO";
            case 5: return "vivo";
            case 6: return "三星";
            case 7: return "荣耀";
            default: return "未知品牌";
        }
    }
}

// 通用响应对象
class Result<T> {
    private Integer code;
    private String message;
    private T data;
    private Boolean ok;

    public Result(Integer code, String message, T data, Boolean ok) {
        this.code = code;
        this.message = message;
        this.data = data;
        this.ok = ok;
    }

    // 快速创建成功响应的方法
    public static <T> Result<T> success(T data) {
        return new Result<>(200, "成功", data, true);
    }

    // Getters and Setters
    public Integer getCode() { return code; }
    public void setCode(Integer code) { this.code = code; }
    public String getMessage() { return message; }
    public void setMessage(String message) { this.message = message; }
    public T getData() { return data; }
    public void setData(T data) { this.data = data; }
    public Boolean getOk() { return ok; }
    public void setOk(Boolean ok) { this.ok = ok; }
}

// 分页数据对象
class PageData {
    private List<Trademark> trademarkList;
    private List<Attr> attrsList;
    private List<Goods> goodsList;
    private Long total;
    private Integer pageSize;
    private Integer pageNo;
    private Integer totalPages;

    public PageData(List<Trademark> trademarkList,
                    List<Attr> attrsList,
                    List<Goods> goodsList,
                    Long total,
                    Integer pageSize,
                    Integer pageNo) {
        this.trademarkList = trademarkList;
        this.attrsList = attrsList;
        this.goodsList = goodsList;
        this.total = total;
        this.pageSize = pageSize;
        this.pageNo = pageNo;
        this.totalPages = (int) Math.ceil((double) total / pageSize);
    }

    // Getters and Setters
    public List<Trademark> getTrademarkList() { return trademarkList; }
    public void setTrademarkList(List<Trademark> trademarkList) { this.trademarkList = trademarkList; }
    public List<Attr> getAttrsList() { return attrsList; }
    public void setAttrsList(List<Attr> attrsList) { this.attrsList = attrsList; }
    public List<Goods> getGoodsList() { return goodsList; }
    public void setGoodsList(List<Goods> goodsList) { this.goodsList = goodsList; }
    public Long getTotal() { return total; }
    public void setTotal(Long total) { this.total = total; }
    public Integer getPageSize() { return pageSize; }
    public void setPageSize(Integer pageSize) { this.pageSize = pageSize; }
    public Integer getPageNo() { return pageNo; }
    public void setPageNo(Integer pageNo) { this.pageNo = pageNo; }
    public Integer getTotalPages() { return totalPages; }
    public void setTotalPages(Integer totalPages) { this.totalPages = totalPages; }
}

// 品牌对象
class Trademark {
    private Integer tmId;
    private String tmName;

    public Trademark(Integer tmId, String tmName) {
        this.tmId = tmId;
        this.tmName = tmName;
    }

    // Getters and Setters
    public Integer getTmId() { return tmId; }
    public void setTmId(Integer tmId) { this.tmId = tmId; }
    public String getTmName() { return tmName; }
    public void setTmName(String tmName) { this.tmName = tmName; }
}

// 属性对象
class Attr {
    private Integer attrId;
    private String attrName;
    private List<String> attrValueList;

    public Attr(Integer attrId, String attrName, List<String> attrValueList) {
        this.attrId = attrId;
        this.attrName = attrName;
        this.attrValueList = attrValueList;
    }

    // Getters and Setters
    public Integer getAttrId() { return attrId; }
    public void setAttrId(Integer attrId) { this.attrId = attrId; }
    public String getAttrName() { return attrName; }
    public void setAttrName(String attrName) { this.attrName = attrName; }
    public List<String> getAttrValueList() { return attrValueList; }
    public void setAttrValueList(List<String> attrValueList) { this.attrValueList = attrValueList; }
}

// 商品对象
class Goods {
    private Long id;
    private String defaultImg;
    private String title;
    private BigDecimal price;
    private Date createTime;
    private Integer tmId;
    private String tmName;
    private Long category1Id;
    private String category1Name;
    private Long category2Id;
    private String category2Name;
    private Long category3Id;
    private String category3Name;
    private Integer hotScore;
    private Object attrs;  // 根据原始JSON，这里可以是null或更复杂的结构

    // 完整构造方法
    public Goods(Long id, String defaultImg, String title, BigDecimal price,
                 Date createTime, Integer tmId, String tmName,
                 Long category1Id, String category1Name,
                 Long category2Id, String category2Name,
                 Long category3Id, String category3Name,
                 Integer hotScore, Object attrs) {
        this.id = id;
        this.defaultImg = defaultImg;
        this.title = title;
        this.price = price;
        this.createTime = createTime;
        this.tmId = tmId;
        this.tmName = tmName;
        this.category1Id = category1Id;
        this.category1Name = category1Name;
        this.category2Id = category2Id;
        this.category2Name = category2Name;
        this.category3Id = category3Id;
        this.category3Name = category3Name;
        this.hotScore = hotScore;
        this.attrs = attrs;
    }

    // Getters and Setters
    public Long getId() { return id; }
    public void setId(Long id) { this.id = id; }
    public String getDefaultImg() { return defaultImg; }
    public void setDefaultImg(String defaultImg) { this.defaultImg = defaultImg; }
    public String getTitle() { return title; }
    public void setTitle(String title) { this.title = title; }
    public BigDecimal getPrice() { return price; }
    public void setPrice(BigDecimal price) { this.price = price; }
    public Date getCreateTime() { return createTime; }
    public void setCreateTime(Date createTime) { this.createTime = createTime; }
    public Integer getTmId() { return tmId; }
    public void setTmId(Integer tmId) { this.tmId = tmId; }
    public String getTmName() { return tmName; }
    public void setTmName(String tmName) { this.tmName = tmName; }
    public Long getCategory1Id() { return category1Id; }
    public void setCategory1Id(Long category1Id) { this.category1Id = category1Id; }
    public String getCategory1Name() { return category1Name; }
    public void setCategory1Name(String category1Name) { this.category1Name = category1Name; }
    public Long getCategory2Id() { return category2Id; }
    public void setCategory2Id(Long category2Id) { this.category2Id = category2Id; }
    public String getCategory2Name() { return category2Name; }
    public void setCategory2Name(String category2Name) { this.category2Name = category2Name; }
    public Long getCategory3Id() { return category3Id; }
    public void setCategory3Id(Long category3Id) { this.category3Id = category3Id; }
    public String getCategory3Name() { return category3Name; }
    public void setCategory3Name(String category3Name) { this.category3Name = category3Name; }
    public Integer getHotScore() { return hotScore; }
    public void setHotScore(Integer hotScore) { this.hotScore = hotScore; }
    public Object getAttrs() { return attrs; }
    public void setAttrs(Object attrs) { this.attrs = attrs; }
}
